﻿using UnityEngine;
using System.IO;
using UnityEditor;
using UnityEngine.UI;
using System.Collections.Generic;

namespace Example_0000_Atlas_01
{
	

public class BuildAssetbundleEditor : Editor {
	public override void OnInspectorGUI() {
		base.OnInspectorGUI();
		
	}

	[MenuItem ("MyMenu/Example_0000_Atlas_02/Build Assetbundle")]
	static private void BuildAssetBundle()
	{
		string dir = Application.dataPath +"/StreamingAssets/Example_0000_Atlas_02";
 
		if(!Directory.Exists(dir)){
			Directory.CreateDirectory(dir);
		}
		List<Sprite> assets = new List<Sprite>();

		DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath +"/Textures/Example_0000_Atlas_02");
		foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
			
			string path = dir +"/"+dirInfo.Name+".assetbundle";
			foreach (FileInfo pngFile in dirInfo.GetFiles("*.png",SearchOption.AllDirectories)) 
			{
				string allPath = pngFile.FullName;
				string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
				//assets.Add(Resources.LoadAssetAtPath<Sprite>(assetPath));
				assets.Add(AssetDatabase.LoadAssetAtPath<Sprite>(assetPath));
			}
			// if(BuildPipeline.BuildAssetBundle(null,
			// 								assets.ToArray(),
			// 								path,
			// 								BuildAssetBundleOptions.UncompressedAssetBundle| BuildAssetBundleOptions.CollectDependencies,
			// 								GetBuildTarget()))
			// {
			// 	Debug.Log("BuildAssetBundle success");
			// }//已过时

			if(BuildPipeline.BuildAssetBundle(null,
											assets.ToArray(),
											path,
											BuildAssetBundleOptions.UncompressedAssetBundle| BuildAssetBundleOptions.CollectDependencies,
											GetBuildTarget()))
			{
				Debug.Log("BuildAssetBundle success");
			}	
			
		}


	}
		static private BuildTarget GetBuildTarget()
		{
			BuildTarget target = BuildTarget.WebPlayer;
#if UNITY_STANDALONE
			target = BuildTarget.StandaloneWindows;
#elif UNITY_IPHONE
			target = BuildTarget.iPhone;
#elif UNITY_ANDROID
			target = BuildTarget.Android;
#endif
			return target;
		}
}
}
